package phone.example.com.a36kr.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.media.Image;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import phone.example.com.a36kr.R;
import phone.example.com.a36kr.activity.DetailedLoginActivity;
import phone.example.com.a36kr.activity.MyCollectActivity;
import phone.example.com.a36kr.activity.RegisterActivity;
import phone.example.com.a36kr.activity.welcome.WelcomeActivity;
import phone.example.com.a36kr.other.CircleDrawable;
import phone.example.com.a36kr.other.mybmob.MyBmobUser;
import phone.example.com.a36kr.tools.BaseFragment;

/**
 * Created by dllo on 16/8/15.
 */
public class MyFragment extends BaseFragment {


    private ImageView iconIm;
    private LocalBroadcastManager localBroadcastManager;
    private BroadcastReceiver mReceiver;
    // 我收到的登录用户的ObjectId
    private String nameId;
    private TextView loginName;
    private MyBmobUser bmobUser;
    private LinearLayout collectLl;

    @Override
    protected int setLayout() {
        return R.layout.fragment_my;
    }

    @Override
    protected void initView() {
        // 用户头像
        iconIm = bindView(R.id.my_icon_im);
        // 用户名
        loginName = bindView(R.id.my_login_name);

        LinearLayout welcome = bindView(R.id.welecome);
        welcome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), WelcomeActivity.class);
                startActivity(intent);
            }
        });
        // 收藏
        collectLl = bindView(R.id.my_collect_essay);

        collectLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyBmobUser user = MyBmobUser.getCurrentUser(MyBmobUser.class);
                if(user != null){
                    Intent intent = new Intent(getActivity(), MyCollectActivity.class);
                    startActivity(intent);
                }else {
                    // 如果没登录就跳转注册界面
                    Intent intent = new Intent(getActivity(), RegisterActivity.class);
                    startActivity(intent);
                }

            }
        });
        // 点击跳转注册界面
        LinearLayout loginLl = bindView(R.id.my_Login_ll);

        loginLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bmobUser = MyBmobUser.getCurrentUser(MyBmobUser.class);
                if (bmobUser != null) {
                    //注册了 就进入用户详情界面
                    Intent intent = new Intent(getActivity(), DetailedLoginActivity.class);
                    startActivity(intent);

                } else {
                    // 如果没登录就跳转注册界面
                    Intent intent = new Intent(getActivity(), RegisterActivity.class);
                    startActivity(intent);
                }

            }

        });


    }

    @Override
    protected void initData() {


    }

    @Override
    public void onResume() {
        super.onResume();
        MyBmobUser user = MyBmobUser.getCurrentUser(MyBmobUser.class);
        try {
            if (user.getUsername() != null) {
                loginName.setText(user.getUsername());
                Bitmap bitmap = bmobUser.getIcon();
                if (bitmap != null) {
                    CircleDrawable circleDrawable = new CircleDrawable(bitmap);
                    iconIm.setImageDrawable(circleDrawable);
                }
            }
        } catch (Exception e) {

        }

    }

    private void initBmob() {
        //你可以使用缓存的CurrentUser对象。缓存的用户有效期为1年。
        bmobUser = MyBmobUser.getCurrentUser(MyBmobUser.class);
        if (bmobUser != null) {
            // 允许用户使用应用
            nameId = bmobUser.getObjectId();
            BmobQuery<MyBmobUser> query = new BmobQuery<MyBmobUser>();

            /**
             * 根据登录用户的ObjectId 查找他的数据
             */
//            query.getObject(nameId, new QueryListener<MyBmobUser>() {
//                @Override
//                public void done(MyBmobUser myBmobUser, BmobException e) {
//                    if (e == null) {
//                        //给用户设置数据
//                        loginName.setText(myBmobUser.getUsername());
//                        Log.d("MyFragment", "登录成功的用户名" + myBmobUser.getUsername());
//                        Bitmap bitmap = bmobUser.getIcon();
//                        if (bitmap != null) {
//                            CircleDrawable circleDrawable = new CircleDrawable(bitmap);
//                            iconIm.setImageDrawable(circleDrawable);
//
//                        }
//                    }
//                }
//            });
        }

    }

    //创建
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBmob();
        //广播接收器
        localBroadcastManager = LocalBroadcastManager.getInstance(getContext());

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.36kr.android.out");
        intentFilter.addAction("com.36kr.android.Login");
        intentFilter.addAction("com.36kr.android.updateHand");
        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                Log.d("MyFragment", "广播已经注册");
                switch (action) {
                    case "com.36kr.android.out":
                        Log.d("退出成功", "我接收到了广播");
                        loginName.setText("未登录");
                        iconIm.setImageDrawable(getResources().getDrawable(R.mipmap.common_avatar));
                        break;
                    case "com.36kr.android.Login":
                        initBmob();
//                        ImageView head = bindView(R.id.detailed_icon_iv);
//                        head.setImageBitmap(null);
                        Log.d("登录成功", "我接收到了广播");
                        break;
                    case "com.36kr.android.updateHand":
                        Bitmap bitmap = bmobUser.getIcon();
                        if (bitmap != null) {
                            iconIm.setImageBitmap(null);
                            iconIm.setImageBitmap(bitmap);
                            Log.d("我接收到了广播", "图片更新成功");
                        }
                        break;
                }
            }
        };

        localBroadcastManager.registerReceiver(mReceiver, intentFilter);

    }

    //销毁
    @Override
    public void onDestroy() {
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(mReceiver);
    }




}
